//
//  xsHadAddMaterialListView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
//  选中的素材列表
//

#import "xsHadAddMaterialListView.h"
  
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

@interface xsHadAddMaterialListView()<UIScrollViewDelegate,xsHadAddMaterialListCollectionViewProtoCol>

//@property (nonatomic,assign)NSInteger collectionType;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *xsXbutton;// x按钮


//@property (nonatomic, strong) UIScrollView *buttonsBgScrollView;
//@property (nonatomic, strong) NSArray *buttons;
//@property (nonatomic, strong) UIView *buttonsBgView;
//@property (nonatomic,strong) UIView *xsColorContourLineView;

@property (nonatomic,strong)UIScrollView *myMusicAuthorityBgScrollView;
@property (nonatomic,strong)xsHadAddMaterialListCollectionView *allCollectionV;// 全部
//@property (nonatomic,strong)xsHadAddMaterialListCollectionView *xsImgCollectionV;// 图片
//@property (nonatomic,strong)xsHadAddMaterialListCollectionView *xsVideoCollectionV;//视频

// 素材库添加按钮
@property (nonatomic, strong) UIButton *xsMaterialPackAddbutton;

@end

@implementation xsHadAddMaterialListView



- (instancetype)initWithSelectMaterialData:(NSArray *)selectMaterialDataArray{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        
//        self.collectionType = collectionType;
        [self addSubview:self.topView];
        
        [self.topView addSubview:self.searchButton];
        [self.topView addSubview:self.topTitleLabel];
        [self.topView addSubview:self.xsXbutton];
        
//        [self addSubview:self.buttonsBgScrollView];
//            
//        [self.buttonsBgScrollView addSubview:self.buttonsBgView];
//        [self.buttonsBgView addSubview:self.xsColorContourLineView];
        
//
        [self addSubview:self.myMusicAuthorityBgScrollView];
        
        [self.myMusicAuthorityBgScrollView addSubview:self.allCollectionV];
        self.allCollectionV.materialDataArray = selectMaterialDataArray;
//        [self.myMusicAuthorityBgScrollView addSubview:self.xsImgCollectionV];
//        [self.myMusicAuthorityBgScrollView addSubview:self.xsVideoCollectionV];
            
//            // 创建按钮数组
//        self.buttons = buttonTitles;//@[@"Tab1", @"Tab2", @"Tab3"];
//            
          
        [self.myMusicAuthorityBgScrollView addSubview:self.xsMaterialPackAddbutton];
        
    }
    return self;
}


//- (UIView *)buttonsBgView{
//    if(!_buttonsBgView){
//        _buttonsBgView = [[UIView alloc] init];
//        _buttonsBgView.backgroundColor = UIColor.blackColor;
//    }
//    return _buttonsBgView;
//}


//@property (nonatomic,strong) UIScrollView *buttonsBgScrollView;
//[self.bgView addSubview:self.buttonsBgScrollView];
//[self.buttonsBgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//    make.edges.equalTo(self.bgView);
//}];
//- (UIScrollView *)buttonsBgScrollView
//{
//    if(!_buttonsBgScrollView){
//        _buttonsBgScrollView = [[UIScrollView alloc] init];
//        _buttonsBgScrollView.backgroundColor = UIColor.clearColor;
//        
//        _buttonsBgScrollView.delegate = self;
//        _buttonsBgScrollView.pagingEnabled = YES;
//        _buttonsBgScrollView.showsHorizontalScrollIndicator = NO;
//    }
//    return _buttonsBgScrollView;
//}



-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    
}


- (void)xsChangeFrame{
    
    CGFloat margin10 = 10;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(margin10);
        make.top.equalTo(self.mas_top);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-margin10);
        make.height.mas_equalTo(50);
    }];
    
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.xsXbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.topView.mas_right);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
    }];
    
    
    
//    self.buttonsBgScrollView.frame = CGRectMake(0, 50+margin10, self.bounds.size.width, 40);
    
    
//    [self.allCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.mas_left).offset(margin10);
//        make.top.equalTo(self.buttonsBgScrollView.mas_bottom).offset(margin10);
//        make.right.equalTo(self.mas_right).offset(-margin10);
//        make.bottom.equalTo(self.mas_bottom).offset(-margin10);
//    }];
    CGFloat myMusicAuthorityBgScrollViewY = 50;// CGRectGetMaxY(self.buttonsBgScrollView.frame)+margin10;
    CGFloat myMusicAuthorityBgScrollViewX = margin10;
    CGFloat myMusicAuthorityBgScrollViewW = self.width-myMusicAuthorityBgScrollViewX*2.0;
    CGFloat myMusicAuthorityBgScrollViewH = self.height - myMusicAuthorityBgScrollViewY-10;
    self.myMusicAuthorityBgScrollView.frame = CGRectMake(myMusicAuthorityBgScrollViewX, myMusicAuthorityBgScrollViewY, myMusicAuthorityBgScrollViewW, myMusicAuthorityBgScrollViewH);
    self.myMusicAuthorityBgScrollView.backgroundColor = UIColor.clearColor;
    
    
    CGFloat xsMaterialPackAddbuttonW = self.myMusicAuthorityBgScrollView.width-10.0;
    CGFloat xsMaterialPackAddbuttonH = 40.0;
    CGFloat xsMaterialPackAddbuttonX = 5.0;
    CGFloat xsMaterialPackAddbuttonY = self.myMusicAuthorityBgScrollView.height-xsMaterialPackAddbuttonH-5.0;
    self.xsMaterialPackAddbutton.frame = CGRectMake(xsMaterialPackAddbuttonX, xsMaterialPackAddbuttonY, xsMaterialPackAddbuttonW, xsMaterialPackAddbuttonH);
    self.xsMaterialPackAddbutton.layer.cornerRadius = 20.1;
    self.xsMaterialPackAddbutton.layer.masksToBounds = YES;
    
    self.allCollectionV.frame = CGRectMake(0, 0, self.myMusicAuthorityBgScrollView.width, self.xsMaterialPackAddbutton.frame.origin.y-10);
//    self.xsImgCollectionV.frame = CGRectMake(self.myMusicAuthorityBgScrollView.width, 0, self.myMusicAuthorityBgScrollView.width, self.myMusicAuthorityBgScrollView.height);
//    self.xsVideoCollectionV.frame = CGRectMake(self.myMusicAuthorityBgScrollView.width*2.0, 0, self.myMusicAuthorityBgScrollView.width, self.myMusicAuthorityBgScrollView.height);
    
    self.myMusicAuthorityBgScrollView.contentSize = CGSizeMake(self.myMusicAuthorityBgScrollView.width, 0);
    
    UIFont *btnFont = Font(16);
    
    
//    UIButton *lastButton;
//    // 添加按钮到滚动视图
//    for (int i = 0; i < self.buttons.count; i++) {
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        
//        NSString *buttonTitleStr = self.buttons[i];
//        
//        CGFloat buttonW = [buttonTitleStr calculateWidth:btnFont withHeight:40]+25.0;
//        
////        button.frame = CGRectMake(i * buttonW, 0, buttonW, 40);
//       
//        button.backgroundColor = UIColor.clearColor;//xrandomColor;
//        [button setTitle:self.buttons[i] forState:UIControlStateNormal];
//        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
//        button.font = btnFont;
//        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//        [button setTag:i];
//        [self.buttonsBgView addSubview:button];
//        
//        
//        if(lastButton){
//            
//            button.frame = CGRectMake(CGRectGetMaxX(lastButton.frame), 0, buttonW, 40);
//            button.font = btnFont;
//        }
//        else{
//            button.frame = CGRectMake(0, 0, buttonW, 40);
//            
//            CGFloat xsColorContourLineViewX = CGRectGetMinX(button.frame)+(buttonW-30.0)*0.5;
//            [self.xsColorContourLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.bottom.equalTo(button.mas_bottom);
//                make.left.equalTo(self.buttonsBgView.mas_left).offset(xsColorContourLineViewX);
//                make.height.mas_equalTo(3.0);
//                make.width.mas_equalTo(30.0);
//            }];
//            self.xsColorContourLineView.layer.cornerRadius = 1.51;
//            self.xsColorContourLineView.layer.masksToBounds = YES;
//            button.font = FontBoldMT(16);
//        }
//        lastButton = button;
//    }
//
//    // 设置内容视图的尺寸
//    self.buttonsBgView.frame = CGRectMake(0, 0, CGRectGetMaxX(lastButton.frame), 40);
//    self.buttonsBgScrollView.contentSize = self.buttonsBgView.frame.size;
    
    [self xsReloadCollectionViewData];
}

- (void)xsReloadCollectionViewWithDataArray:(NSArray *)dArray{
    
//    [self.allCollectionV reloadData];
    self.allCollectionV.materialDataArray = dArray;
    [self.allCollectionV reloadData];
}
- (void)xsReloadCollectionViewData{
    
    [self.allCollectionV reloadData];
}

//- (void)buttonClicked:(UIButton *)sender {
//    NSInteger selectedIndex = sender.tag;
//    NSLog(@"Selected index: %ld", (long)selectedIndex);
//    
//    // 在这里切换底部视图
//    
//    
////    if(selectedIndex==0){// 我的
////        
////    }
////    else{// 官方
////
////    }
//    
//    CGFloat xsColorContourLineViewX = CGRectGetMinX(sender.frame)+(sender.width-30.0)*0.5;
//    for (UIButton *tempButton in self.buttonsBgView.subviews) {
//        if([tempButton  isKindOfClass:[UIButton class]]){
//            if(tempButton.tag!=sender.tag){
//                tempButton.font = Font(16);
//            }
//            else{
//                tempButton.font = FontBoldMT(16);
//            }
//        }
//    }
//    
//    
//    //CGFloat xsColorContourLineViewX2 = CGRectGetMinX(sender.frame)+(sender.width-30.0)*0.5;
//    
//    
//    XSWeakSelf;
//    [UIView animateWithDuration:0.3 animations:^{
////        [weakSelf.xsColorContourLineView mas_updateConstraints:^(MASConstraintMaker *make) {
////            make.left.equalTo(weakSelf.buttonsBgView.mas_left).offset(xsColorContourLineViewX);
////        }];
//        [weakSelf.xsColorContourLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(sender.mas_bottom);
//            make.left.equalTo(self.buttonsBgView.mas_left).offset(xsColorContourLineViewX);
//            make.height.mas_equalTo(3.0);
//            make.width.mas_equalTo(30.0);
//        }];
//        
//        [weakSelf.myMusicAuthorityBgScrollView setContentOffset:CGPointMake(weakSelf.myMusicAuthorityBgScrollView.width*selectedIndex, 0)];
//    }];
//    
//}

- (void)updateConstraints{
    [super updateConstraints];
//    
//    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
    
    
    
}


- (xsHadAddMaterialListCollectionView *)allCollectionV{
    
    if(!_allCollectionV){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
//        NSArray *tempArr = @[@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓",@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓"];
//        
//        NSMutableArray *tempMarr = [NSMutableArray new];
//        for (int i =0; i<30; i++) {
//            xsSelectAddMaterialCollectionViewCellModel *md = [xsSelectAddMaterialCollectionViewCellModel new];
//            
//            int tpIndex = arc4random()%(tempArr.count);
//            md.titleString = [NSString stringWithFormat:@"%@",tempArr[tpIndex]];
//            md.headImg = @"bossIPDefaultImage";
//            [tempMarr addObject:md];
//        }
//        
        //collectionType;// 0全部 1图片 2视频
        _allCollectionV = [[xsHadAddMaterialListCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withCollectionType:0];
        _allCollectionV.backgroundColor = UIColor.blackColor;
        _allCollectionV.delegateConfigurationMusic = self;
        
    }
    return _allCollectionV;
}


//- (xsHadAddMaterialListCollectionView *)xsImgCollectionV{
//    
//    if(!_xsImgCollectionV){
//        
//        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
//        // 3. 横向间距
//        la.minimumInteritemSpacing = 5.0;
//        // 4. 纵向间距
//        la.minimumLineSpacing = 5.0;
//        
////        NSArray *tempArr = @[@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓",@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓"];
////        
////        NSMutableArray *tempMarr = [NSMutableArray new];
////        for (int i =0; i<30; i++) {
////            xsSelectAddMaterialCollectionViewCellModel *md = [xsSelectAddMaterialCollectionViewCellModel new];
////            
////            int tpIndex = arc4random()%(tempArr.count);
////            md.titleString = [NSString stringWithFormat:@"%@",tempArr[tpIndex]];
////            md.headImg = @"bossIPDefaultImage";
////            [tempMarr addObject:md];
////        }
//        
//        ////audioType;// 0我的 1官方
//        _xsImgCollectionV = [[xsHadAddMaterialListCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withCollectionType:3];
//        _xsImgCollectionV.backgroundColor = UIColor.blackColor;
//        _xsImgCollectionV.delegateConfigurationMusic = self;
//        
//    }
//    return _xsImgCollectionV;
//}

//- (xsHadAddMaterialListCollectionView *)xsVideoCollectionV{
//    
//    if(!_xsVideoCollectionV){
//        
//        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
//        // 3. 横向间距
//        la.minimumInteritemSpacing = 5.0;
//        // 4. 纵向间距
//        la.minimumLineSpacing = 5.0;
//        
////        NSArray *tempArr = @[@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓",@"温柔妹子",@"开朗小杰",@"萌丫头",@"鸡汤妹妹",@"温柔晓晓"];
////
////        NSMutableArray *tempMarr = [NSMutableArray new];
////        for (int i =0; i<30; i++) {
////            xsSelectAddMaterialCollectionViewCellModel *md = [xsSelectAddMaterialCollectionViewCellModel new];
////
////            int tpIndex = arc4random()%(tempArr.count);
////            md.titleString = [NSString stringWithFormat:@"%@",tempArr[tpIndex]];
////            md.headImg = @"bossIPDefaultImage";
////            [tempMarr addObject:md];
////        }
//        
//        ////audioType;// 0我的 1官方
//        _xsVideoCollectionV = [[xsHadAddMaterialListCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withCollectionType:1];
//        _xsVideoCollectionV.backgroundColor = UIColor.blackColor;
//        _xsVideoCollectionV.delegateConfigurationMusic = self;
//        
//    }
//    return _xsVideoCollectionV;
//}

- (UIView *)topView{
    
    if(!_topView){
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = UIColor.clearColor;
    }
    return _topView;
}



- (UIButton *)xsXbutton{
    if (!_xsXbutton) {
        _xsXbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsXbutton.backgroundColor = UIColor.clearColor;
        [_xsXbutton addTarget:self action:@selector(xsXbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsXbutton setImage:[UIImage imageNamed:@"xsBossgou"] forState:UIControlStateNormal];//xsBossIPX
    }
    return _xsXbutton;
}
- (void)xsXbuttonAction:(UIButton *)btn{
    NSLog(@"xsXbuttonAction\n\n");
   
    if([self.delegate respondsToSelector:@selector(xsHadAddMaterialListVXbuttonEvent:)]){
        [self.delegate xsHadAddMaterialListVXbuttonEvent:btn];
    }
}

- (UIButton *)searchButton{
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchButton.backgroundColor = UIColor.purpleColor;
        [_searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchButton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        _searchButton.hidden = YES;
    }
    return _searchButton;
}
- (void)searchButtonAction:(UIButton *)btn{
    NSLog(@"searchButtonAction\n\n");
   
    
}
- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = [UIFont systemFontOfSize:21];// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        _topTitleLabel.text = @"选中素材列表";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}



- (UIScrollView *)myMusicAuthorityBgScrollView
{
    if(!_myMusicAuthorityBgScrollView){
        _myMusicAuthorityBgScrollView = [[UIScrollView alloc] init];
        _myMusicAuthorityBgScrollView.backgroundColor = UIColor.clearColor;
        _myMusicAuthorityBgScrollView.showsVerticalScrollIndicator = NO;
        _myMusicAuthorityBgScrollView.showsHorizontalScrollIndicator = NO;
        _myMusicAuthorityBgScrollView.pagingEnabled = YES;
        _myMusicAuthorityBgScrollView.delegate = self;
    }
    return _myMusicAuthorityBgScrollView;
}
//- (UIView *)xsColorContourLineView{
//    if(!_xsColorContourLineView){
//        _xsColorContourLineView = [[UIView alloc] init];
//        _xsColorContourLineView.backgroundColor = COLOR_White;
//    }
//    return _xsColorContourLineView;
//}


#pragma mark  ScollViewDelegate
// 滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"scrollViewDidEndDecelerating");
//    if(scrollView==self.myMusicAuthorityBgScrollView){
//        int pageIndex = scrollView.contentOffset.x/self.myMusicAuthorityBgScrollView.frame.size.width;
//        
//        NSLog(@"scrollViewDidEndDecelerating--pageIndex==%d\n",pageIndex);
//        for (UIButton *tempButton in self.buttonsBgView.subviews) {
//            if([tempButton isKindOfClass:[UIButton class]])
//            {
//                if(pageIndex==tempButton.tag){
//                    [self buttonClicked:tempButton];
//                    break;
//                }
//            }
//        }
//    }
}

#pragma mark xsHadAddMaterialListCollectionViewProtoCol
//collectionType;// 0全部 1图片 2视频
- (void)xsHadAddMaterialListCollectionCellBeClick:(xsSelectAddMaterialCollectionViewCellModel *)model withCollectionType:(NSInteger)collectionType{
    
    if(self.xsHadAddMaterialListCellCallbackBlock){
        self.xsHadAddMaterialListCellCallbackBlock(model,collectionType);
    }
}

//collectionType;// 0全部 3图片 1视频
- (void)xsHadAddMaterialListCollectionCellTopXbuttonBeClick:(xsSelectAddMaterialCollectionViewCellModel *)model withCollectionType:(NSInteger)collectionType{
    
    if(self.xsHadAddMaterialListCellTopXbuttonCallbackBlock){
        self.xsHadAddMaterialListCellTopXbuttonCallbackBlock(model,collectionType);
    }
}

- (void)xsHadAddMaterialListDeleteWithModel:(xsSelectAddMaterialCollectionViewCellModel *)dModel withCollectionType:(NSInteger)collecionTyp{
    
    
    NSMutableArray *tempMarr = [NSMutableArray arrayWithArray:self.allCollectionV.materialDataArray];
    self.allCollectionV.materialDataArray = @[];
    
    xsSelectAddMaterialCollectionViewCellModel *findModel;
    
    for (xsSelectAddMaterialCollectionViewCellModel *tempModel in tempMarr) {
        if(tempModel.Id==dModel.Id){
            findModel = tempModel;
            break;
        }
    }
    if(findModel){
        [tempMarr removeObject:findModel];
    }
    self.allCollectionV.materialDataArray = [NSArray arrayWithArray:tempMarr];
    
    [self.allCollectionV reloadData];
}

// 素材库添加按钮
- (UIButton *)xsMaterialPackAddbutton{
    if (!_xsMaterialPackAddbutton) {
        _xsMaterialPackAddbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsMaterialPackAddbutton.backgroundColor = COLOR_Blue;
        [_xsMaterialPackAddbutton addTarget:self action:@selector(xsMaterialPackAddbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        //[_xsMaterialPackAddbutton setImage:[UIImage imageNamed:@"xsBossIPX"] forState:UIControlStateNormal];
        [_xsMaterialPackAddbutton setTitle:kLocalizedString(@"素材库添加") forState:UIControlStateNormal];
        [_xsMaterialPackAddbutton setTitleColor:COLOR_textWhiteColor forState:UIControlStateNormal];
        _xsMaterialPackAddbutton.font = Font(16);
    }
    return _xsMaterialPackAddbutton;
}
- (void)xsMaterialPackAddbuttonAction:(UIButton *)btn{
    NSLog(@"xsMaterialPackAddbuttonAction\n\n");
   
    if([self.delegate respondsToSelector:@selector(xsHadAddMaterialPackAddbuttonbuttonEvent:)]){
        [self.delegate xsHadAddMaterialPackAddbuttonbuttonEvent:btn];
    }
}

@end
